package com.mini.constant;

import java.util.Date;

/**
 * Created with meng.
 * User: meng5
 * Date: 2019-11-27
 * Description:
 */
public enum ClassState {
    class_create("CREATED", "已创建"),
    class_start("STARTED", "已开学"),
    class_end("END", "已结课"),
    class_abolition("ABOLITION", "废弃"),
    class_pigeonhole("PIGEONHOLE", "已归档"),
    ;

    private String code;
    private String name;

    ClassState(String code, String name) {
        this.code = code;
        this.name = name;
    }

    public String getCode() {
        return code;
    }

    public String getName() {
        return name;
    }

    public static ClassState getByCode(Integer code){
        if(code == null){
            return null;
        }
        for(ClassState cs : ClassState.values()){
            if(cs.getCode().equals(code)){
                return cs;
            }
        }
        return null;
    }

    /**
     * 根据开始结束时间判断状态
     * @param classStartTime
     * @param classEndTime
     * @return
     */
    public static ClassState compareCurrent(Date classStartTime, Date classEndTime){
        ClassState cs = null;
        long current = System.currentTimeMillis();
        if(classEndTime != null){
            long end = classEndTime.getTime();
            if(current >= end){
                return ClassState.class_end;
            }
        }
        if(classStartTime != null){
            long start = classStartTime.getTime();
            if(current > start){
                return ClassState.class_start;
            }
        }

        return ClassState.class_create;
    }
}